import React from 'react'
import { Result, Icon, Tools, Toast } from 'weaver-mobile';

const getLabel = Tools.getLabel;

// api请求错误统一处理方法
const GroupVoteConstant = {
	
	// api请求错误统一处理方法
  catchApiError: (result) => {
    return new Promise((resolve, reject) => {
      if (!result.api_status) {
        Toast.fail(result.msg || getLabel(30651, '操作失败'));
        reject(result);
        return;
      }
      resolve(result);
    });
  },
}

export default GroupVoteConstant;

/* 暂无投票 页面 */
export const renderNoSearch = () => {
  return (
    <div className='group-vote-noSearch'>
      <Result
        img={<Icon type="no-data" />}
        message={getLabel('83320','没有可显示的数据')}
      />
    </div>
  )
}

/* 暂无查看权限 页面 */
export const renderNotExist = () => {
  return (
    <div className='group-vote-noNotExist'>
      <Result
        img={<Icon type="wuquanxian" />}
        message={getLabel('516176','投票不存在')}
      />
    </div>
  )
}

/* Loading 页面 */
export const renderLoading = (
  <div className='group-vote-loading'>
    <div className='group-vote-loading-content'>
      <Icon type='loading' size='lg' />
      <div style={{ marginTop: 8 }}>loading...</div>
    </div>
  </div>
);

export const FormatDate = (time, str) => {
  let obj = {
    yyyy: time.getFullYear(),
    MM: ('0' + (time.getMonth() + 1)).slice(-2),
    dd: ('0' + time.getDate()).slice(-2),
    HH: ('0' + time.getHours()).slice(-2),
    mm: ('0' + time.getMinutes()).slice(-2),
    ss: ('0' + time.getSeconds()).slice(-2),
  }
  return str.replace(/([a-z]+)/ig, v => obj[v])
}

export const urlParse = (url) => {
  let obj = {};
  let keyvalue = [];
  let key = "",
      value = "";
  let stringReg = /[^?&]+=[^?&]+/g;
  let paraString = url.match(stringReg);
  for (let i = 0; i < paraString.length; i++) {
    keyvalue = paraString[i].split("=");
    key = keyvalue[0];
    value = keyvalue[1];
    obj[key] = value;
  }
  return obj;
}